<?php
	
	include_once("Login.php");

	
	
	$conn = new mysqli($host,$user,$passwd, $database)
	
	or die("SQL connect error: ".mysqli_connect_error());
	
	

	$body = file_get_contents('php://input');
	$xml = simplexml_load_string($body);
	$cloudId = $xml->cloudId;	
	
	
	
	$query = "SELECT m.Id as id, m.ToUserId as toid, m.FromUserId as fromid, t.Username as toname, f.Username as fromname, m.Message as message, m.DateSent as date, m.ToKeep as tokeep, m.FromKeep as fromkeep FROM Friends f JOIN User ut ON f.FromUserId = ut.Id JOIN User uf ON uf.Id = f.FromUserId JOIN Places pt on pt.CloudId = ut.Id JOIN Places pf on pf.CloudId = uf.If WHERE (f.ToUserId='$cloudId' OR f.FromUserId='$cloudId') and pf.IsCurrentLocation = 1 and pt.IsCurrentLocation = 1";
	//"SELECT * FROM Message JOIN User t JOIN User f ON Message.ToUserId = User.Id WHERE Message.ToUserId='$userId' OR Message.FromUserId='$userId'";

	
	$result = mysqli_query($conn, $query);
	echo "<node>";
	while($row = mysqli_fetch_array($result))
	
	{
		
		
	echo "<newmessage>";
			echo "<id>";
			
				echo $row['id'] . "\n";		
			echo "</id>";
		
			echo "<toid>
";		
				echo $row['toid'] . "\n";			
			echo "</toid>"
;			
			echo "<toname>"
;			
				echo $row['toname'] . "\n";			
			echo "</toname>
";			
			echo "<fromid>"
;			
				echo $row['fromid'] . "\n";

		
			echo "</fromid>
";			
			echo "<fromname>"
;			
				echo $row['fromname'] . "\n";

			
			echo "</fromname>
";	
			echo "<message>
";			
				echo $row['message'] . "\n";		
			echo "</message>
";	
			echo "<date>
";		
				echo $row['date'] . "\n";			
			echo "</date>"
;	
			echo "<tokeep>"
;			
				echo $row['tokeep'] . "\n";			
			echo "</tokeep>"
;	
			echo "<fromkeep>"
;			
				echo $row['fromkeep'];			
			echo "</fromkeep>";
			echo "</newmessage>";
			
			
	}
	
	echo "</node>";	
		mysqli_close($conn);

?>


